#include "BPageSwitchContainer.h"
#include <QPushButton>


BPageSwitchContainer::BPageSwitchContainer(QWidget *parent) :
    QWidget(parent)
{
    this->resize(parent->size());

    m_page[0] = new BPageSwitch(this);
    m_page[1] = new BPageSwitch(this);
    m_page[0]->setID(0);
    m_page[1]->setID(1);
    //设置按钮透明度
    m_opacity = new QGraphicsOpacityEffect;
    m_opacity->setOpacity(1);
    for (int i = 0; i < m_pageNum; i++) {
        m_page[i]->setGraphicsEffect(m_opacity);
    }

    QLabel* label1 = new QLabel(m_page[0]);
    label1->setText("Page1");
    label1->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    m_page[0]->layout()->addWidget(label1);

    QLabel* label2 = new QLabel(m_page[1]);
    label2->setText("Page2");
    label2->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    m_page[1]->layout()->addWidget(label2);

    QPushButton* button = new QPushButton(this);
    button->setText("Test");
    button->resize(100, 50);
    button->move(this->width()/2, this->height() - button->height());
    button->show();

//    widget = new QWidget(this);
//    widget->resize(m_elseSize);
//    widget->move(m_elsePoint);
//    widget->setStyleSheet("background-color: rgb(170, 183, 184 );border-radius:12px;");
//    connect(button, &QPushButton::clicked, this, [&](){
//      widget->resize(widget->size() + QSize(100, 100));
//    });

//    m_page[0]->resize(m_centerSize);
//    m_page[0]->move(m_centerPoint);
//    m_page[1]->resize(m_elseSize);
//    m_page[1]->move(m_elsePoint);
    setCenterPage(0);
    for (int i = 0; i < m_pageNum; i++) {
        connect(m_page[i], &BPageSwitch::requestCenter, this, &BPageSwitchContainer::setCenterPage);
    }
}

BPageSwitchContainer::~BPageSwitchContainer()
{
    for (auto& p : m_page) {
        delete p;
    }
}


void BPageSwitchContainer::setCenterPage(int pageID)
{
    m_centerPageID = pageID;
    /*
    * @Brief: 动画
    */
    int elseID = (pageID == 1 ? 0 : 1);
    //进入中心的动画
    QParallelAnimationGroup* dpGroup = new QParallelAnimationGroup;
    QPropertyAnimation* animToCenterPos = new QPropertyAnimation(m_page[pageID], "pos", this);
    QPropertyAnimation* animToCenterSize = new QPropertyAnimation(m_page[pageID], "size", this);
    //退出中心的动画
    QPropertyAnimation* animToElsePos = new QPropertyAnimation(m_page[elseID], "pos", this);
    QPropertyAnimation* animToElseSize = new QPropertyAnimation(m_page[elseID], "size", this);
    //淡入透明度动画
    QPropertyAnimation* fade = new QPropertyAnimation(m_opacity, "opacity");
    fade->setDuration(750);
    fade->setStartValue(0);
    fade->setEndValue(0.99);
    //toCenterPos
    animToCenterPos->setStartValue(m_page[pageID]->pos());
    animToCenterPos->setEndValue(m_centerPoint);
    animToCenterPos->setDuration(600);
    animToCenterPos->setEasingCurve(QEasingCurve::InOutExpo);
    //toElsePos
    animToElsePos->setStartValue(m_page[elseID]->pos());
    animToElsePos->setEndValue(m_elsePoint);
    animToElsePos->setDuration(600);
    animToElsePos->setEasingCurve(QEasingCurve::InOutExpo);
    //toCenterSize
    animToCenterSize->setStartValue(m_page[pageID]->size());
    animToCenterSize->setEndValue(m_centerSize);
    animToCenterSize->setDuration(600);
    animToCenterSize->setEasingCurve(QEasingCurve::InOutExpo);
    //toElseSize
    animToElseSize->setStartValue(m_page[elseID]->size());
    animToElseSize->setEndValue(m_elseSize);
    animToElseSize->setDuration(600);
    animToElseSize->setEasingCurve(QEasingCurve::InOutExpo);
    //动画组
    dpGroup->addAnimation(fade);
    dpGroup->addAnimation(animToCenterPos);
    dpGroup->addAnimation(animToCenterSize);
    dpGroup->addAnimation(animToElsePos);
    dpGroup->addAnimation(animToElseSize);
    //设置状态
    m_page[pageID]->setCenter();
    m_page[elseID]->resetCenter();
    //启动动画
    dpGroup->start();
    //bug，指针没有销毁
//    connect(dpGroup, &QParallelAnimationGroup::stateChanged, this, [&](){
//        delete dpGroup;
//        delete animToCenterPos;
//        delete animToCenterSize;
//        delete animToElsePos;
//        delete animToElseSize;
//        delete fade;
//    });
}

